package com.jiangkai.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jiangkai.common.bean.Rest;
import com.jiangkai.common.controller.SuperController;
import com.jiangkai.entity.system.SysRole;
import com.jiangkai.entity.system.SysUser;
import com.jiangkai.entity.system.SysUserRole;
import com.jiangkai.model.system.ISysDeptService;
import com.jiangkai.model.system.ISysRoleService;
import com.jiangkai.model.system.ISysUserRoleService;
import com.jiangkai.model.system.ISysUserService;

/**
 * 用户管理
 * @author jiangkia
 *
 */
@Controller
@RequestMapping("/system/yhgl")
public class UserController extends SuperController{  

	@Autowired 
	private ISysUserService sysUserService;
	@Autowired 
	private ISysRoleService sysRoleService;
	@Autowired 
	private ISysUserRoleService sysUserRoleService;
	@Autowired 
	private ISysDeptService sysDeptService;
	
    @RequestMapping("/index")  
    public  String index(Model model){
    	model.addAttribute("pageData", null);
		return "system/yhgl/user_index";
    } 
	
	/**
	 * 分页查询用户
	 */
    @ResponseBody
    @RequestMapping("/list")  
    public  Map<String, Object> findbypage(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,@RequestParam(defaultValue="15") Integer pageSize,String name,Model model){
		if(StringUtils.isNotBlank(name)){
			model.addAttribute("search", name);
		}
    	Page<Map<Object, Object>> page = getPage(pageNo,pageSize);
    	model.addAttribute("pageSize", pageSize);
    	Page<Map<Object, Object>> pageData = sysUserService.selectUserPage(page, name);
//    	model.addAttribute("pageData", pageData);
    	return getBootstrapData(pageData);
    } 
    /**
     * 新增用户
     */
	@RequiresPermissions("addUser")
    @RequestMapping("/add")  
    public  String add(Model model){
    	model.addAttribute("roleList", sysRoleService.selectList(null));
    	model.addAttribute("deptList", sysDeptService.selectList(null));
		return "system/user/add";
    } 
    
    /**
     * 执行新增
     */
    @RequiresPermissions("addUser")
    @RequestMapping("/doAdd")  
    @ResponseBody
    public  Rest doAdd(SysUser user,@RequestParam(value="roleId[]",required=false) String[] roleId){
    	
    	sysUserService.insertUser(user,roleId);
    	return Rest.ok();
    }  
    /**
     * 删除用户
     */
    @RequiresPermissions("deleteUser")
    @RequestMapping("/delete")  
    @ResponseBody
    public  Rest delete(String id){
    	sysUserService.delete(id);
    	return Rest.ok();
    }  
    
	/**
	 * 编辑用户
	 */
    @RequestMapping("/edit/{id}")  
    @RequiresPermissions("editUser")
    public  String edit(@PathVariable String id,Model model){
    	SysUser sysUser = sysUserService.selectById(id);
    	
    	List<SysRole> sysRoles = sysRoleService.selectList(null);
    	EntityWrapper<SysUserRole> ew = new EntityWrapper<SysUserRole>();
    	ew.eq("userId ", id);
    	List<SysUserRole> mySysUserRoles = sysUserRoleService.selectList(ew);
    	List<String> myRolds = new ArrayList<String>();
    	
    	for (int i = 0; i < mySysUserRoles.size(); i++) {
    		myRolds.add(mySysUserRoles.get(i).getRoleId());
		}
    	model.addAttribute("sysUser",sysUser);
    	model.addAttribute("sysRoles",sysRoles);
    	model.addAttribute("myRolds",myRolds);
    	model.addAttribute("deptList", sysDeptService.selectList(null));
    	return "system/user/edit";
    } 
    /**
     * 执行编辑
     */
    @RequiresPermissions("editUser")
    @RequestMapping("/doEdit")  
    @ResponseBody
    public  Rest doEdit(SysUser sysUser,@RequestParam(value="roleId[]",required=false) String[] roleId,Model model){
    	sysUserService.updateUser(sysUser,roleId);
    	return Rest.ok();
    } 
    
    /**
     * 验证用户名是否已存在
     */
    @RequestMapping("/checkName")  
    @ResponseBody
    public Rest checkName(String userName){
    	List<SysUser> list = sysUserService.selectList(new EntityWrapper<SysUser>().eq("userName", userName));
    	if(list.size() > 0){
    		return Rest.failure("用户名已存在");
    	}
    	return Rest.ok();
    }
    
}
